<?php

/**
 * bookmark actions.
 *
 * @package    Bookstore
 * @subpackage bookmark
 * @author     Enkuso
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class bookmarkActions extends sfActions
{
    public function preExecute()
    {
        /**
         * Хэрэглэгч нэвтэрсэн байх ёстой.
         */
        $this->forward404Unless($this->getRequest()->hasParameter('token'));
    }

    /**
     * Bookmark жагсаалтад ном нэмэх
     */
    public function executeAddBookmark(sfWebRequest $request)
    {
        $book_id = $request->getParameter('book_id');
        $form = new sfForm(array(),array(),false);
        $form->setWidget('book_id', new sfWidgetFormInput());
        $form->setValidator('book_id', new sfValidatorDoctrineChoice(array(
            'model'     => 'Book',
            'column'    => 'id'
        )));
        if($request->isMethod('POST')){
            $form->bind(array('book_id' => $book_id));
            if($form->isValid()){
                $user_id = $this->getUser()->getGuardUser()->getId();
                $bookmark = BookmarkTable::getInstance()->findBookmark(
                    $form->getValue('book_id'),
                    $user_id
                );

                if(!$bookmark){
                    $bm_count = BookmarkTable::getInstance()->getUserBookmarkCount($user_id);
                    if($bm_count < sfConfig::get('app_bookmark_limit', 10)){
                        $bookmark = new Bookmark();
                        $bookmark->setBookId($form->getValue('book_id'));
                        $bookmark->setUserId($user_id);
                        $bookmark->save();

                        $this->stat = 'ok';
                    } else {
                        $this->stat = 'fail';
                        $this->error_code = 23;
                        $this->error_msg = apiErrors::getErrorMessage($this->error_code);
                    }
                } else {
                    $this->stat = 'fail';
                    $this->error_code = 22;
                    $this->error_msg = apiErrors::getErrorMessage($this->error_code);
                }
            } else {
                $this->stat = 'fail';
                $this->error_code = 1;
                $this->error_msg = apiErrors::getErrorMessage($this->error_code);
            }
        } else {
            $this->forward404();
        }
    }

    /**
     * Bookmark жагсаалт авах
     */
    public function executeGetBookmark(sfWebRequest $request)
    {
        $bookmark = BookmarkTable::getInstance()->findByUserId($this->getUser()->getGuardUser()->getId());

        $this->books = array();
        if($bookmark && count($bookmark)){
            foreach($bookmark as $bm){
                $book = $bm->getBook();
                $this->books[$book->getId()] = $book->shortInfoAsArray();
            }
            $this->total = count($bookmark);
            $this->stat = 'ok';
        } else {
            $this->stat = 'fail';
            $this->error_code = 21;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
        }
    }

    /**
     * Bookmark жагсаалтаас хасах
     */
    public function executeDeleteBookmark(sfWebRequest $request)
    {
        $book_id = $request->getParameter('book_id');
        $user_id = $this->getUser()->getGuardUser()->getId();

        $form = new sfForm(array(),array(),false);
        $form->setWidget('book_id', new sfWidgetFormInput());
        $form->setValidator('book_id', new sfValidatorDoctrineChoice(array(
            'model'     => 'Book',
            'column'    => 'id'
        )));

        if($request->isMethod('POST') || $request->isMethod('DELETE')){

            $form->bind(array('book_id' => $book_id));

            if($form->isValid()){
                $bookmark = BookmarkTable::getInstance()->findBookmark($book_id, $user_id);
                if($bookmark){
                    $bookmark->delete();
                    $this->stat = 'ok';
                } else {
                    $this->stat = 'fail';
                    $this->error_code = 24;
                    $this->error_msg = apiErrors::getErrorMessage($this->error_code);
                }
            } else {
                $this->stat = 'fail';
                $this->error_code = 1;
                $this->error_msg = apiErrors::getErrorMessage($this->error_code);
            }
        } else {
            $this->forward404();
        }
    }
}
